//
//  Category.m
//  We365
//
//  Created by Pham Khuong Duy on 12/13/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "Category.h"
#import "DBManager.h"

@implementation Category
@synthesize CategoryID;
@synthesize CategoryName;
@synthesize SearchKey;

- (id)init
{
    self = [super init];
    if (self) {
        list = [[NSMutableArray alloc] init];
    }
    
    return self;
}

- (void)getAllCategories
{
    [DBManager openDB];
    char *getAll = "SELECT * FROM CATEGORY;";
    sqlite3_stmt *stmt;
    sqlite3_prepare_v2([DBManager getDB], getAll, -1, &stmt, nil);
    while (sqlite3_step(stmt) == SQLITE_ROW) {
        Category *category = [[Category alloc] init];
        category.CategoryID = sqlite3_column_int(stmt, 0);
        char *name = (char *)sqlite3_column_text(stmt, 1);
        category.CategoryName = [NSString stringWithUTF8String:name];
        char *key = (char *)sqlite3_column_text(stmt, 2);
        category.SearchKey = [NSString stringWithUTF8String:key];
        [self.list addObject:category];
        [category release];
    }
    sqlite3_finalize(stmt);
    [DBManager closeDB];
}

- (void)dealloc
{
    [list release];
    [super dealloc];
}

@end
